//
//  LKMineViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/4/13.
//  Copyright © 2018年 she1323. All rights reserved.
//


#import "LKMineViewController.h"
#import "LKMineHeaderView.h"
#import "LKMineCell.h"
#import "LKMIneCardViewController.h"
#import "LKMineAchieveInformViewController.h"
#import "LKInstrumentBoardViewController.h"
#import "LKChartsListViewController.h"
#import "LKVisitRecordViewController.h"
#import "LKSwitchRootVcTool.h"
#import "LKDayInforPersonInforEditViewController.h"
#import "LKDailyChartViewController.h"
#import "LKDailyChartTeamViewController.h"
@interface LKMineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)NSArray *titlesArray;
@property(nonatomic,strong)NSArray *imagesArray;
@property(nonatomic,strong)UIButton *exitBtn;

@end

@implementation LKMineViewController

-(LKBaseTableview *)tableview{
    if (!_tableview) {
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - TAB_BAR_HEIGHT) style:UITableViewStyleGrouped registercellClasss:@[@"LKMineCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = cellHeaderBgColor;
    }
    return _tableview;
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.backButton.hidden = YES;
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    CGFloat offset = 0;
//    [self.navigationController.navigationBar
//     setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:201/255.0 blue:31/255.0 alpha:(offset / 64)>0.99?0.99:(offset / 64)]] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar
//     setShadowImage:[UIImage new]];
    [self.tableview reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
}
-(UIImage*) imageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    [self getMineCardAccountDetailInformation];
}
/**
 获取用户详情
 */
-(void)getMineCardAccountDetailInformation{
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:UserDetailUrl header:[LKUserInforTool header] parameters:nil progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        LKUserInfor *userInfor = [LKUserInfor accountWithDict:responseObject[@"result"]];
        userInfor.token = [LKUserInforTool account].token;
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:userAccountCaculationUrl header:[LKUserInforTool header] parameters:nil progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            [self.tableview.mj_header endRefreshing];
            NSString *customerCout = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"cusertomerCount"]];
            NSString *investMoney = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"orderInverstment"]];
            NSString *investFundMoney = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"orderInverstmentFund"]];
            userInfor.cusertomerCount = customerCout;
            userInfor.orderInverstment = investMoney;
            userInfor.orderInverstmentFund = investFundMoney;
            [LKUserInforTool saveAccount:userInfor];
            [self.tableview reloadData];
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
        
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
/**
 尾部上拉加载更多
 */
//-(void)footerReloadData{
//
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
//    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    
    [self.tableview reloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadPersonalInformation:) name:reloadUserInformationNotice object:nil];
    // Do any additional setup after loading the view.
}

/**
 通知刷新用户信息

 */
-(void)reloadPersonalInformation:(NSNotification *)notice{
    [self.tableview reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titlesArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKMineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMineCell"];
    cell.titleLabel.text = self.titlesArray[indexPath.section];
    cell.iconImageView.image = [UIImage imageNamed:self.imagesArray[indexPath.section]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0 ? 282:0.000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 120;
//    if (section == 2) {
//        return 120;
//    }
//    return section == 0 ? 1 : 12;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        LKUserInfor *user = [LKUserInforTool account];
        LKMineHeaderView *view = [[[NSBundle mainBundle] loadNibNamed:@"LKMineHeaderView" owner:self options:nil] lastObject];
        view.nameLabel.text = user.name;
        view.codeLabel.text = [NSString stringWithFormat:@"%@ | %@",[LKUserInforTool account].postName,[LKUserInforTool account].userSerialNumber];
        [view.touxiangImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,user.headPortrait]] placeholderImage:[UIImage imageNamed:@"minecardtouxiang"]];
        view.touxiangImageView.userInteractionEnabled = YES;
        view.touxiangImageView.layer.cornerRadius = 32.5;
        view.touxiangImageView.layer.masksToBounds = YES;
        view.customerCountLabel.text = user.cusertomerCount;
        view.investMoneyLabel.text = user.orderInverstment;
        view.investFoundMoneyLabel.text = user.orderInverstmentFund;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editUserInformation:)];
        [view.touxiangImageView addGestureRecognizer:tap];
        view.backgroundColor = cellHeaderBgColor;
        [[view.zsmpBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            LKMIneCardViewController *vc = [[LKMIneCardViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }];
        return view;
    }
    return [[UIView alloc] init];
   
}

/**
 点击头像编辑个人用户资料

 */
-(void)editUserInformation:(UITapGestureRecognizer *)tap{
    LKDayInforPersonInforEditViewController *vc = [[LKDayInforPersonInforEditViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = cellHeaderBgColor;
    if (section == 0) {
        self.exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [footerView addSubview:self.exitBtn];
        [self.exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(footerView.mas_centerX);
            make.top.equalTo(footerView.mas_top).offset(50);
            make.height.mas_equalTo(50);
            make.left.equalTo(footerView.mas_left).offset(60);
        }];
        [self.exitBtn setTitle:@"退出" forState:UIControlStateNormal];
        self.exitBtn.backgroundColor = blueDefault;
        self.exitBtn.layer.cornerRadius = 25;
        self.exitBtn.layer.masksToBounds = YES;
        [self.exitBtn addTarget:self action:@selector(customerExitActoion:) forControlEvents:UIControlEventTouchUpInside];
        return footerView;
    }
     return footerView;
}
-(void)customerExitActoion:(UIButton *)btn{
    [LKUserInforTool noAccount];
    [LKSwitchRootVcTool switchRootController];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            LKVisitRecordViewController *vc = [[LKVisitRecordViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
            
            
//            LKMineAchieveInformViewController *vc = [[LKMineAchieveInformViewController alloc] init];
//            [self.navigationController pushViewController:vc animated:YES];
//            LKChartsListViewController *vc = [[LKChartsListViewController alloc] init];
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
//            LKInstrumentBoardViewController *vc1 = [[LKInstrumentBoardViewController alloc] initWithNibName:@"LKInstrumentBoardViewController" bundle:nil];
//            [self.navigationController pushViewController:vc1 animated:YES];
        }
            break;
        case 2:{
//            LKVisitRecordViewController *vc = [[LKVisitRecordViewController alloc] init];
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

-(NSArray *)titlesArray{
    if (!_titlesArray) {
//        _titlesArray = @[@"业务报表统计",@"业务指标",@"拜访记录"];
        _titlesArray = @[@"拜访记录"];
    }
    return _titlesArray;
}
-(NSArray *)imagesArray{
    if (!_imagesArray) {
//        _imagesArray = @[@"yjbbtj",@"yjzb",@"bfjl"];
         _imagesArray = @[@"bfjl"];
    }
    return _imagesArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
